package com.nd.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.OrderGoodsMapper;
import com.nd.dao.OrderMapper;
import com.nd.dao.UserMapper;
import com.nd.pojo.bean.*;
import com.nd.pojo.bo.OrderRefundBO;
import com.nd.pojo.bo.OrderShipBO;
import com.nd.pojo.vo.OrderDetailVO;
import com.nd.pojo.vo.OrderListVO;
import com.nd.pojo.vo.wx.WxOrderListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-05 23:16
 **/
@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;


    @Override
    public OrderListVO orderList(Integer page, Integer limit, Short orderStatusArray, String sort, String order, String orderSn, Integer userId) {

        List<OrderListVO.ItemsBean> orderListVOList = new ArrayList<>();
        PageHelper.startPage(page, limit);
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(sort + " " + order);
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null && !"".equals(userId)) criteria.andUserIdEqualTo(userId);
        if (orderSn != null && !"".equals(orderSn)) criteria.andOrderSnEqualTo(orderSn);
        if (orderStatusArray != null && !"".equals(orderStatusArray)) criteria.andOrderStatusEqualTo(orderStatusArray);
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        long total = orderPageInfo.getTotal();
        OrderListVO orderListVO = new OrderListVO();
        orderListVO.setTotal(total);
        for (Order order1 : orderList) {
            OrderListVO.ItemsBean build = OrderListVO.ItemsBean.builder().id(order1.getId()).userId(order1.getUserId()).orderSn(order1.getOrderSn()).orderStatus(order1.getOrderStatus()).consignee(order1.getConsignee()).mobile(order1.getMobile()).address(order1.getAddress()).message(order1.getMessage()).goodsPrice(order1.getGoodsPrice()).freightPrice(order1.getFreightPrice()).couponPrice(order1.getCouponPrice()).integralPrice(order1.getIntegralPrice()).grouponPrice(order1.getGrouponPrice()).orderPrice(order1.getOrderPrice()).actualPrice(order1.getActualPrice()).comments(order1.getComments()).endTime(order1.getEndTime()).addTime(order1.getAddTime()).updateTime(order1.getUpdateTime()).deleted(order1.getDeleted()).shipSn(order1.getShipSn()).shipChannel(order1.getShipChannel()).shipTime(order1.getShipTime()).build();
            orderListVOList.add(build);
        }
        orderListVO.setItems(orderListVOList);
        return orderListVO;
    }

    @Override
    public OrderDetailVO orderDetail(Integer id) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        //order
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteriaOrder = orderExample.createCriteria();
        criteriaOrder.andIdEqualTo(id);
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        for (Order order : orderList) {
            OrderDetailVO.OrderBean orderBean = OrderDetailVO.OrderBean.builder().id(order.getId()).userId(order.getUserId()).orderSn(order.getOrderSn()).orderStatus(order.getOrderStatus()).consignee(order.getConsignee()).mobile(order.getMobile()).address(order.getAddress()).message(order.getMessage()).goodsPrice(order.getGoodsPrice()).freightPrice(order.getFreightPrice()).couponPrice(order.getCouponPrice()).integralPrice(order.getIntegralPrice()).grouponPrice(order.getGrouponPrice()).orderPrice(order.getOrderPrice()).actualPrice(order.getActualPrice()).comments(order.getComments()).endTime(order.getEndTime()).addTime(order.getAddTime()).updateTime(order.getUpdateTime()).deleted(order.getDeleted()).build();
            orderDetailVO.setOrder(orderBean);
        }

        //orderGoods
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteriaOrderGoods = orderGoodsExample.createCriteria();
        criteriaOrderGoods.andOrderIdEqualTo(orderList.get(0).getId());
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
        List<OrderDetailVO.OrderGoodsBean> orderGoodsBeanList = new ArrayList<>();
        for (OrderGoods orderGoods : orderGoodsList) {
            OrderDetailVO.OrderGoodsBean orderGoodsBean = OrderDetailVO.OrderGoodsBean.builder().id(orderGoods.getId()).orderId(orderGoods.getOrderId()).goodsId(orderGoods.getGoodsId()).goodsName(orderGoods.getGoodsName()).goodsSn(orderGoods.getGoodsSn()).productId(orderGoods.getProductId()).number(orderGoods.getNumber()).price(orderGoods.getPrice()).specifications(orderGoods.getSpecifications()).picUrl(orderGoods.getPicUrl()).comment(orderGoods.getComment()).addTime(orderGoods.getAddTime()).updateTime(orderGoods.getUpdateTime()).deleted(orderGoods.getDeleted()).build();
            orderGoodsBeanList.add(orderGoodsBean);
        }
        orderDetailVO.setOrderGoods(orderGoodsBeanList);

        //user
        UserExample userExample = new UserExample();
        UserExample.Criteria criteriaUser = userExample.createCriteria();
        criteriaUser.andIdEqualTo(orderList.get(0).getUserId());
        List<User> users = userMapper.selectByExample(userExample);
        for (User user : users) {
            OrderDetailVO.UserBean userBean = OrderDetailVO.UserBean.builder().nickname(user.getNickname()).avatar(user.getAvatar()).build();
            orderDetailVO.setUser(userBean);
        }
        return orderDetailVO;
    }

    @Override
    public void orderRefund(OrderRefundBO orderRefundBO) {
        Integer orderStatus = 203;
        orderMapper.orderRefund(orderRefundBO,orderStatus);
    }

    @Override
    public void orderShip(OrderShipBO orderShipBO) {
        //shipChannel shipSn orderStatus updateTime
        orderMapper.orderShip(orderShipBO);
    }

    @Override
    public WxOrderListVO orderList(Integer showType, Integer page, Integer limit) {


        return null;
    }
}
